/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * cadCompraJInternalFrame.java
 *
 * Created on 03/11/2009, 19:12:14
 */
package br.com.fatecpg.zanotti.jframes.cadastros;

import br.com.fatecpg.zanotti.acessobanco.conComboFornecedor;
import br.com.fatecpg.zanotti.acessobanco.conComboLoja;
import br.com.fatecpg.zanotti.acessobanco.conComboProduto;
import br.com.fatecpg.zanotti.acessobanco.conTableCompra;
import br.com.fatecpg.zanotti.acessobanco.dmlEncomenda;
import br.com.fatecpg.zanotti.jframes.consultas.conCompraJInternalFrame;
import br.com.fatecpg.zanotti.modelo.Encomenda;
import br.com.fatecpg.zanotti.modelo.Sessao;

/**
 *
 * @author Luiz Zanotti
 */
public class cadCompraJInternalFrame extends javax.swing.JInternalFrame {

    /** Creates new form cadCompraJInternalFrame */
    public cadCompraJInternalFrame() {
        initComponents();
        atualizaCodigo();

        conComboFornecedor conComboForn = new conComboFornecedor();
        fornecedorjComboBox.removeAllItems();
        fornecedorjComboBox.setModel(conComboForn.consulta());


        conComboLoja con = new conComboLoja();
        String[] teste = con.consulta();
        lojajComboBox.removeAllItems();
        lojajComboBox.addItem(teste[0]);
        lojajComboBox.addItem(teste[1]);

    }

    public void atualizaCodigo() {
        conTableCompra con = new conTableCompra();
        codigojTextField.setText(con.ConsultaMAXEncomenda());
    }

    public void Estoquista(boolean b){
        fornecedorjComboBox.setEnabled(!b);
        produtojComboBox.setEnabled(!b);
        lojajComboBox.setEnabled(!b);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        codigojLabel = new javax.swing.JLabel();
        codigojTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        fornecedorjComboBox = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        produtojComboBox = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        lojajComboBox = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        datajFormattedTextField = new javax.swing.JFormattedTextField();
        jButton1 = new javax.swing.JButton();
        entreguejCheckBox = new javax.swing.JCheckBox();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setTitle("Cadastrar Compra");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jPanel1.setLayout(new java.awt.GridLayout(5, 2));

        codigojLabel.setText("Código");
        jPanel1.add(codigojLabel);

        codigojTextField.setEnabled(false);
        jPanel1.add(codigojTextField);

        jLabel1.setText("Selecione o Fornecedor:");
        jPanel1.add(jLabel1);

        fornecedorjComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        fornecedorjComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fornecedorjComboBoxActionPerformed(evt);
            }
        });
        jPanel1.add(fornecedorjComboBox);

        jLabel2.setText("Selecione o Produto:");
        jPanel1.add(jLabel2);

        produtojComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione Fornecedor" }));
        jPanel1.add(produtojComboBox);

        jLabel3.setText("Loja destino:");
        jPanel1.add(jLabel3);

        lojajComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel1.add(lojajComboBox);

        jLabel4.setText("Data de Entrega:");
        jPanel1.add(jLabel4);

        try {
            datajFormattedTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jPanel1.add(datajFormattedTextField);

        jButton1.setText("Cadastrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        entreguejCheckBox.setText("Recebido");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(entreguejCheckBox)
                    .addComponent(jButton1))
                .addContainerGap(59, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(entreguejCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
conComboFornecedor conF = new conComboFornecedor();
conComboLoja conL = new conComboLoja();
conComboProduto conP = new conComboProduto();

Encomenda.getInstancia().setCDENCOMENDA(codigojTextField.getText());
Encomenda.getInstancia().setCDFORNECEDOR(conF.consultaCDFORNECEDOR(fornecedorjComboBox.getSelectedItem().toString()));
Encomenda.getInstancia().setCDLOJA(conL.consultaCDLOJA(lojajComboBox.getSelectedItem().toString()));
Encomenda.getInstancia().setCDPRODUTO(conP.consultaCDPRODUTO(produtojComboBox.getSelectedItem().toString()));
Encomenda.getInstancia().setDTENTREGA(datajFormattedTextField.getText());

if(entreguejCheckBox.isSelected())Encomenda.getInstancia().setICENTREGUE("S");
else Encomenda.getInstancia().setICENTREGUE("N");
boolean b=false;

if(jButton1.getText().equals("Cadastrar"))b=dmlEncomenda.cadastrar(Encomenda.getInstancia());
else b=dmlEncomenda.atualizar(Encomenda.getInstancia());

if(b){
datajFormattedTextField.setText("");
atualizaCodigo();

lojajComboBox.setSelectedIndex(0);
fornecedorjComboBox.setSelectedIndex(0);
produtojComboBox.setSelectedIndex(0);
setEnabledCombos(b);
this.setTitle("Cadastro de Compras");
this.jButton1.setText("Cadastrar");
}
conCompraJInternalFrame.getInstancia().atualizaTable();

if(Sessao.getInstance().getCDCARGO().equals("4")){this.setVisible(false);}

    }//GEN-LAST:event_jButton1ActionPerformed
int counter=0;

    private static cadCompraJInternalFrame p;

    public static cadCompraJInternalFrame getInstancia() {
        if (p == null) {
            p = new cadCompraJInternalFrame();
        }
        return p;
    }

    public void setEnabledCombos(boolean b){
    produtojComboBox.setEnabled(b);
    lojajComboBox.setEnabled(b);
    fornecedorjComboBox.setEnabled(b);
    }

    private void fornecedorjComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fornecedorjComboBoxActionPerformed

        if(counter!=0){
        conComboProduto con = new conComboProduto();

        conComboFornecedor conF = new conComboFornecedor();


        produtojComboBox.setModel(con.consulta(conF.consultaCDFORNECEDOR(fornecedorjComboBox.getSelectedItem().toString())));
        }
        counter++;

    }//GEN-LAST:event_fornecedorjComboBoxActionPerformed
public void setCadastrarJButton(String s){

jButton1.setText(s);

}
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel codigojLabel;
    private javax.swing.JTextField codigojTextField;
    private javax.swing.JFormattedTextField datajFormattedTextField;
    private javax.swing.JCheckBox entreguejCheckBox;
    private javax.swing.JComboBox fornecedorjComboBox;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JComboBox lojajComboBox;
    private javax.swing.JComboBox produtojComboBox;
    // End of variables declaration//GEN-END:variables

    /**
     * @param s
     */
    public void setCodigojTextField(String s) {
        this.codigojTextField.setText(s);
    }

    /**
     * @param datajFormattedTextField the datajFormattedTextField to set
     */
    public void setDatajFormattedTextField(String s) {
        this.datajFormattedTextField.setText(s);
    }

    /**
     * @param entreguejCheckBox the entreguejCheckBox to set
     */
    public void setEntreguejCheckBox(String s) {
        if(s.equals("S"))this.entreguejCheckBox.setSelected(true);
        else this.entreguejCheckBox.setSelected(false);
    }

    /**
     * @param fornecedorjComboBox the fornecedorjComboBox to set
     */
    public void setFornecedorjComboBox(String s) {
        conComboFornecedor con = new conComboFornecedor();
        this.fornecedorjComboBox.setSelectedItem(con.consultaNMFORNECEDOR(s));
    }

    /**
     * @param lojajComboBox the lojajComboBox to set
     */
    public void setLojajComboBox(String s) {
        this.lojajComboBox.setSelectedIndex(Integer.parseInt(s) - 1);
    }

    /**
     * @param produtojComboBox the produtojComboBox to set
     */
    public void setProdutojComboBox(String s) {
        conComboProduto con = new conComboProduto();
        

        conComboFornecedor conF = new conComboFornecedor();
        produtojComboBox.setModel(con.consulta(conF.consultaCDFORNECEDOR(fornecedorjComboBox.getSelectedItem().toString())));
        this.produtojComboBox.setSelectedItem(con.consultaNMMODELO(s));
    }
}
